package com.gdglc.spring.biz.impl.auth;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.security.config.annotation.authentication.configurers.userdetails.DaoAuthenticationConfigurer;

import com.gdglc.spring.biz.auth.StaffBiz;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.bean.auth.vo.StaffVo;
import com.y2.authprj.dao.auth.IStaffDao;
import com.y2.authprj.exception.BizException;
import com.y2.authprj.exception.UpdateException;
import com.y2.authprj.utils.DES3LEncrypt;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

public class StaffBizImpl implements StaffBiz{

	private IStaffDao IStaffDao;
	
	
	
	public IStaffDao getIStaffDao() {
		return IStaffDao;
	}



	public void setIStaffDao(IStaffDao iStaffDao) {
		IStaffDao = iStaffDao;
	}


	@Override
	public List<StaffInfo> findAll() throws BizException {
		List<StaffInfo> staffInfos=IStaffDao.findAll();
		if (staffInfos==null) {
			//就抛出没有改角色的存在的异常处理
			throw new BizException("没有该用户的存在");
		}
		return staffInfos;
	}



	@Override
	public int addStaff(StaffInfo staffInfo) throws BizException{
		// TODO Auto-generated method stub
		int i=findByStaffNum(staffInfo.getStaffNum());
		
		if (staffInfo.getMobile().length()>0) {
			if (staffInfo.getMobile().length()!=11) {
				throw new BizException("手机必须为11位数");
			}
		}

		
		if (i==0) {
/*			String szSrc = "123456789:"+staffInfo.getPwd();
			DES3LEncrypt.getInstance();
			try {
				byte[] encrypt2 = DES3LEncrypt.encrypt(szSrc);
				String encodedString =new String(encrypt2,"utf-8");
				System.out.println(encodedString);
				
		        String value = DES3LEncrypt.decrypt(encodedString);//调用解密方法  
		        value =  value.substring(value.indexOf(':')+1);
		        System.out.println(value);
				staffInfo.setPwd(encodedString);
				
			} catch (Exception e) {
				e.printStackTrace();
			}*/		
			return IStaffDao.addStaff(staffInfo);
		}else{
			throw new BizException("用户账号不能重复");
		}
	}



	@Override
	public int updateStaff(StaffInfo staffInfo) throws BizException{
		
		int i = 0;
		try{
			i = IStaffDao.updateStaff(staffInfo);
			System.out.println(i);
		}catch(Exception e){
			throw new BizException("修改失败",e);
		}
		if(i==0){
			throw new UpdateException("您修改的记录已经不存在");
		}
		return i;
		
		
/*		int i=findByPwd(staffInfo.getPwd());
		
		if (i==0) {
			String szSrc = "123456789:"+staffInfo.getPwd();			
			DES3LEncrypt.getInstance();
			
			try {
				byte[] encrypt2 = DES3LEncrypt.encrypt(szSrc);
				String encodedString = new String(encrypt2, "utf-8");
				System.out.println(encodedString);

				String value = DES3LEncrypt.decrypt(encodedString);// 调用解密方法
				value = value.substring(value.indexOf(':') + 1);
				System.out.println(value);
				staffInfo.setPwd(encodedString);
				
			}catch(Exception e){
				throw new BizException("修改失败",e);
			}
		}
		return IStaffDao.updateStaff(staffInfo);*/
		/*if(i==0){
			throw new UpdateException("您修改的记录已经不存在");
		}*/
	}



	@Override
	public void deleteStaff(Integer id) throws BizException{
		if(null == id||id==0){
			throw new BizException("删除错误，请指定需要删除的记录");
		}
		try {
			IStaffDao.deleteStaff(id);
		} catch (Exception e) {
			throw new BizException("删除失败",e);
		}
		
	}



	@Override
	public List<StaffInfo> findByStaff(StaffVo staffVo) {
		// TODO Auto-generated method stub
		return IStaffDao.findByStaff(staffVo);
	}

	
	@Override
	public StaffInfo show(Integer staffId) throws BizException {
		if(null == staffId||staffId==0){
			throw new BizException("指定需要修改的记录");
		}
		//测试如果给一个不存在的id，userInfo是null还是一个新对象
		StaffInfo userInfo = IStaffDao.findById(staffId);
		if(null==userInfo||userInfo.getStaffId()!=staffId){
			throw new BizException("获取不到指定记录的信息，请确认是否已经被删除");
		}
		return userInfo;
	}


	//多条件查询 by 辉
	@Override
	public List<StaffInfo> queryByChose(StaffInfo staffInfo) {
		// TODO Auto-generated method stub
		return IStaffDao.queryByChose(staffInfo);
	}
	//多条件查询 by 辉
	@Override
	public PageInfo<StaffInfo> queryByPage(StaffInfo staffInfo, Integer pageNo, Integer pageSize) {
		pageNo = pageNo == null?1:pageNo;//当前页  *必须
	    pageSize = pageSize == null?10:pageSize;//每页的数量* 必须
	    PageHelper.startPage(pageNo, pageSize);//*必须
	    List<StaffInfo> list = IStaffDao.queryByChose(staffInfo);
	    PageInfo<StaffInfo> page = new PageInfo<StaffInfo>(list);
	    return page;
	}



	@Override
	public int findByStaffNum(String staffNum) throws BizException {
		// TODO Auto-generated method stub
			boolean flag=false;			
			int i=0 ;
			i =IStaffDao.findByStaffNum(staffNum);
			
		return i;
		
	}



	@Override
	public int findByStaffChname(@Param("chnName") String chnName,@Param("surName") String surName) throws BizException {
		// TODO Auto-generated method stub
		boolean flag=false;			
		int i=0 ;
		i =IStaffDao.findByStaffChname(chnName,surName);
		
	return i;
	}



	@Override
	public int verifyLoginByStaffNumAndPwd(String staffNum, String pwd) throws BizException{
		if(staffNum!=null && pwd!=null){
			try {
				int i=IStaffDao.verifyLoginByStaffNumAndPwd(staffNum, pwd);
				if(i>0){
					return i;
				}
			} catch (Exception e) {
				throw new BizException("用户名或密码错误",e);
			}
		}
		return -1;
		
	}



	@Override
	public int findByPwd(String pwd) throws BizException {
		boolean flag=false;			
		int i=0 ;
		i =IStaffDao.findByPwd(pwd);
		
	return i;
	}



	@Override
	public StaffInfo findByStaffName(String userName) {
		
		return IStaffDao.findByStaffName(userName);
	}



	@Override
	public int updateIPandTime(StaffInfo staffInfo) {
		// TODO Auto-generated method stub
		return IStaffDao.updateIPandTime(staffInfo);
	}



	@Override
	public StaffInfo findById(Integer id) {
		// TODO Auto-generated method stub
		return IStaffDao.findById(id);
	}
}
